package de.negativrekord.blockworld;

import com.riekoff.particlecubeworld.editor.visual.AVisual;

import cc.creativecomputing.control.CCControl;
import cc.creativecomputing.graphics.CCGraphics;
import cc.creativecomputing.math.CCMath;
import cc.creativecomputing.math.signal.CCPerlinNoise;
import de.negativrekord.blockworld.geometry.BlockCreator;

public class BlockWorld implements AVisual {
	int MAP_W = 100;
	int MAP_H = 100;
	int N_VERTICES = MAP_W * MAP_H * 2 * 24 * 3;
	int W = 20;
	int scale0 = 10;
	int scale1 = 4;
	int[][] map0 = new int[MAP_W / 10][MAP_H / 10];
	int[][] map1 = new int[MAP_W][MAP_H];

	@CCControl(name = "lvl0", min = 1, max = 1000)
	int alt0 = 1;

	@CCControl(name = "lvl1", min = 1, max = 1000)
	int alt1 = 1;

	@CCControl(name = "noise")
	CCPerlinNoise noise = new CCPerlinNoise();

	CameraWrapper _myCameraWrapper;
	CameraPath _myCameraPath = new CameraPath();
	private BlockCreator _myBlockCreator;

	public BlockWorld(CCGraphics g) {
		_myCameraWrapper = new CameraWrapper(g);
		_myBlockCreator = new BlockCreator(N_VERTICES);

		recalc();

		_myCameraWrapper.changeDirXY(0);
		_myCameraWrapper.tiltLR(CCMath.PI);
		recalc();
	}

	@CCControl(name = "calc map")
	public void recalc() {

		for (int i = 0; i < MAP_W / scale0; i++) {
			for (int j = 0; j < MAP_H / scale0; j++) {
				map0[i][j] = (int) (alt0 * noise.value(i * scale0, j * scale0));
				if (map0[i][j] < alt0 / 2)
					map0[i][j] = 0;
			}
		}

		for (int i = 0; i < MAP_W / scale1; i++) {
			for (int j = 0; j < MAP_H / scale1; j++) {
				map1[i][j] = (int) (alt1 * noise.value(i * scale1, j * scale1));
				if (map1[i][j] < alt1 / 2)
					map1[i][j] = 0;
			}
		}

		for (int i = 0; i < MAP_W / scale0; i++) {
			for (int j = 0; j < MAP_H / scale0; j++) {
				_myBlockCreator.addBox(i * W * scale0, j * W * scale0, 0, W * scale0, map0[i][j]);
			}
		}

		// for (int i=0; i<MAP_W/scale1; i++) {
		// for (int j=0; j<MAP_H/scale1; j++) {
		// int ii = i/10;
		// int jj = j/10;
		// _myBlockCreator.addBox(i*W*scale1, j*W*scale1, map0[i/10][j/10]+2,
		// W*scale1, map1[i][j]);
		// }
		// }
	}

	@Override
	public void draw(CCGraphics g) {
		_myCameraWrapper.position(_myCameraPath.currentPos());
		_myCameraWrapper.changeDirXY(_myCameraPath.pan());
		//_myCameraWrapper.tiltUD(_myCameraPath.tilt());
		_myCameraPath.move(3f);
		// g.polygonMode(CCPolygonMode.LINE);
		_myBlockCreator.mesh().draw(g);

		// inside cube
		g.pushMatrix();
		g.rotateX(90);
		g.translate(0, -2000, 0);
		_myBlockCreator.mesh().draw(g);
		g.popMatrix();
		//
		// g.pushMatrix();
		// g.rotateX(270);
		// g.translate(0,-2000,0);
		// _myMesh.draw(g);
		// g.popMatrix();
		//
		// g.pushMatrix();
		// g.rotateY(-90);
		// g.translate(-2000,-2000,-2000);
		// _myMesh.draw(g);
		// g.popMatrix();

		// outside cube
		// g.pushMatrix();
		// g.rotateX(90);
		// g.translate(0,-W*MAP_W,0);
		// _myBlockCreator.mesh().draw(g);
		// g.popMatrix();

		// g.pushMatrix();
		// g.rotateX(270);
		// g.translate(0,-2000,0);
		// _myBlockCreator.mesh().draw(g);
		// g.popMatrix();

		g.pushMatrix();
		g.rotateY(-90);
		g.translate(-W * MAP_W, 0, 0);
		_myBlockCreator.mesh().draw(g);
		g.popMatrix();
	}

}